package com.shanghaionstar.carmanage.service;

import android.content.Context;

import com.onstar.cn.api.GprsPackageService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.PackageType;
import com.shanghaionstar.carmanage.model.OrderRecordResponseEntity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/7/29.
 */
public class OrderRecodService extends BaseService {

    private Context context;

    public OrderRecodService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<OrderRecordResponseEntity> getOrderRecords(final PackageType packageTypes, final Integer pageNum,final Integer pageSize) {
        return Observable.defer(new Func0<Observable<OrderRecordResponseEntity>>() {
            @Override
            public Observable<OrderRecordResponseEntity> call() {
                try {
                    return Observable.just(doGetOrderRecords(packageTypes,pageNum,pageSize));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public OrderRecordResponseEntity doGetOrderRecords(PackageType packageTypes, Integer pageNum, Integer pageSize) throws OnStarException {
        String idpId = ApplicationUtil.getInstance().getIdpID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = null;
        if (!CommonUtil.isNull(ApplicationUtil.getInstance().getCurrentVehicle())) {
            vin = ApplicationUtil.getInstance().getCurrentVehicle().getVin();
        }
        GprsPackageService service = new GprsPackageService(Constants.CURRENT_SERVER_URL,context);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);

        service.getOrderHistory(requestEntity, idpId,accountID, vin, pageNum, pageSize,packageTypes);
        requestEntity =  HttpInjectUtils.injectHttp(getClass(), "doGetOrderRecords", requestEntity,PackageType.class,Integer.class,Integer.class);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));

        return GsonHelper.fromJson(response,OrderRecordResponseEntity.class);
    }
}